import * as Cesium from 'cesium/Cesium'
import getWidgetOrigin from "./getWidgetOrigin";
import RotationEditor from "./RotationEditor";
import TranslationEditor from "./TranslationEditor";
import ScaleEditor from "./ScaleEditor";

var widgetPosition = new Cesium.Cartesian3
    , screenPosition = new Cesium.Cartesian2
    , noScale$4 = new Cesium.Cartesian3(1,1,1)
    , transformScratch$1 = new Cesium.Matrix4
    , vectorScratch$1 = new Cesium.Cartesian3
    , scaleScratch$2 = new Cesium.Cartesian3
    , EditorMode = {
    TRANSLATION: "translation",
    ROTATION: "rotation",
    SCALE: "scale"
}
    , setHprQuaternion = new Cesium.Quaternion
    , setHprQuaternion2 = new Cesium.Quaternion
    , setHprTranslation = new Cesium.Cartesian3
    , setHprScale = new Cesium.Cartesian3
    , setHprCenter = new Cesium.Cartesian3
    , setHprTransform = new Cesium.Matrix4
    , setHprRotation = new Cesium.Matrix3;
function setHeadingPitchRoll(e, t) {
    var i = Cesium.Quaternion.fromHeadingPitchRoll(t, setHprQuaternion)
        , r = Cesium.Matrix4.getTranslation(e, setHprTranslation)
        , n = Cesium.Matrix4.getScale(e, setHprScale)
        , a = Cesium.Matrix4.multiplyByPoint(e, Cesium.Cartesian3.ZERO, setHprCenter)
        , o = Cesium.Transforms.eastNorthUpToFixedFrame(a, void 0, setHprTransform)
        , s = Cesium.Matrix4.getMatrix3(o, setHprRotation)
        , l = Cesium.Quaternion.fromRotationMatrix(s, setHprQuaternion2)
        , c = Cesium.Quaternion.multiply(l, i, s);
    return Cesium.Matrix4.fromTranslationQuaternionRotationScale(r, c, n, e)
}

function TransformEditorViewModel(e) {
    var r = (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).scene
        , t = e.transform
        , i = e.boundingSphere.clone()
        , n = Cesium.defaultValue(e.originOffset, Cesium.Cartesian3.ZERO)
        , a = Cesium.Matrix4.getTranslation(t, new Cesium.Cartesian3)
        , o = Cesium.Transforms.fixedFrameToHeadingPitchRoll(t, r.mapProjection.ellipsoid, void 0, new Cesium.HeadingPitchRoll)
        , s = Cesium.Matrix4.getScale(t, new Cesium.Cartesian3);
    Cesium.Cartesian3.equalsEpsilon(a, Cesium.Cartesian3.ZERO, Cesium.Math.EPSILON10) && (a = Cesium.Cartesian3.fromDegrees(0, 0, 0, r.mapProjection.ellipsoid, a),
        setHeadingPitchRoll(t = Cesium.Matrix4.setTranslation(t, a, t), o));
    var l = !0;
    Cesium.Math.equalsEpsilon(s.x, s.y, Cesium.Math.EPSILON10) && Cesium.Math.equalsEpsilon(s.x, s.z, Cesium.Math.EPSILON10) && (l = !1,
        s.y = s.x,
        s.z = s.x);
    var c = i.radius / Cesium.Cartesian3.maximumComponent(s);
    this.editorMode = void 0;

    var u = Cesium.knockout.observable();
    Cesium.knockout.defineProperty(this, "editorMode", {
        get: function() {
            return u()
        },
        set: function(e) {
            var t;
            u(e),
            Cesium.defined(this._activeEditor) && (this._activeEditor.active = !1),
                e === EditorMode.ROTATION ? t = this._rotationEditor : e === EditorMode.TRANSLATION ? t = this._translationEditor : e === EditorMode.SCALE && (t = this._scaleEditor),
                t.update(),
                t.active = !0,
                this._activeEditor = t
        }
    }),
        this.enableNonUniformScaling = l;
    var d = Cesium.knockout.observable(this.enableNonUniformScaling);
    Cesium.knockout.defineProperty(this, "enableNonUniformScaling", {
        get: function() {
            return d()
        },
        set: function(e) {
            e !== d() && (d(e),
            e || (this.scale = new Cesium.Cartesian3(s.x,s.x,s.x),
            r.requestRenderMode && r.requestRender()))
        }
    }),
        this.position = a;
    var h = Cesium.knockout.observable(this.position);
    Cesium.knockout.defineProperty(this, "position", {
        get: function() {
            return h()
        },
        set: function(e) {
            if (!Cesium.Cartesian3.equals(e, this.position)) {
                var t = Cesium.Cartesian3.clone(e, this.position);
                h(t);
                var i = this._transform;
                setHeadingPitchRoll(i = Cesium.Matrix4.setTranslation(i, t, i), this.headingPitchRoll),
                r.requestRenderMode && r.requestRender()
            }
        }
    }),
        this.headingPitchRoll = o;
    var p = Cesium.knockout.observable(this.headingPitchRoll);
    Cesium.knockout.defineProperty(this, "headingPitchRoll", {
        get: function() {
            return p()
        },
        set: function(e) {
            if (!Cesium.HeadingPitchRoll.equals(e, this.headingPitchRoll)) {
                var t = Cesium.HeadingPitchRoll.clone(e, this.headingPitchRoll);
                p(t),
                    setHeadingPitchRoll(this._transform, t),
                r.requestRenderMode && r.requestRender()
            }
        }
    }),
        this.scale = s;
    var m = Cesium.knockout.observable(this.scale);
    Cesium.knockout.defineProperty(this, "scale", {
        get: function() {
            return m()
        },
        set: function(e) {
            if (!Cesium.Cartesian3.equals(e, this.scale)) {
                var t = Cesium.Cartesian3.clone(e, this.scale);
                m(t),
                    Cesium.Matrix4.setScale(this._transform, t, this._transform),
                    this._translationEditor.update(),
                    this._rotationEditor.update(),
                r.requestRenderMode && r.requestRender()
            }
        }
    }),
        this.menuExpanded = !1,
        this.left = "0",
        this.top = "0",
        this.active = !1,
        Cesium.knockout.track(this, ["menuExpanded", "left", "top", "active"]);
    var f = this;
    this._rotationEditor = new RotationEditor({
        scene: r,
        transform: t,
        radius: c,
        originOffset: n,
        setPosition: function(e) {
            f.position = e
        },
        setHeadingPitchRoll: function(e) {
            f.headingPitchRoll = e
        }
    }),
        this._translationEditor = new TranslationEditor({
            scene: r,
            transform: t,
            radius: c,
            originOffset: n,
            setPosition: function(e) {
                f.position = e
            }
        }),
        this._scaleEditor = new ScaleEditor({
            scene: r,
            transform: t,
            enableNonUniformScaling: d,
            radius: c,
            originOffset: n,
            setScale: function(e) {
                f.scale = e
            },
            setPosition: function(e) {
                f.position = e
            }
        });
        this._sseh = new Cesium.ScreenSpaceEventHandler(r.canvas),
        this._scene = r,
        this._transform = t,
        this._boundingSphere = i,
        this._active = !1,
        this._activeEditor = void 0,
        this._originOffset = n,
        this.position = a,
        this.headingPitchRoll = o,
        this.scale = s,
        this._removePostUpdateEvent = this._scene.preUpdate.addEventListener(TransformEditorViewModel.prototype._update, this)
}
Cesium.defineProperties(TransformEditorViewModel.prototype, {
    originOffset: {
        get: function() {
            return this._originOffset
        },
        set: function(e) {
            this._originOffset = e,
                this._translationEditor.originOffset = e,
                this._rotationEditor.originOffset = e,
                this._scaleEditor.originOffset = e
        }
    }
}),
    TransformEditorViewModel.prototype.setOriginPosition = function(e) {
        var t = Cesium.Matrix4.setScale(this._transform, noScale$4, transformScratch$1)
            , i = Cesium.Matrix4.inverseTransformation(t, t)
            , r = Cesium.Matrix4.multiplyByPoint(i, e, vectorScratch$1)
            , n = Cesium.Cartesian3.divideComponents(r, Cesium.Matrix4.getScale(this._transform, scaleScratch$2), r);
        this.originOffset = n
    }
    ,
    TransformEditorViewModel.prototype.activate = function() {
        var e = this._sseh
            , t = this._scene;
        e.setInputAction(this._leftDown.bind(this), Cesium.ScreenSpaceEventType.LEFT_DOWN),
            e.setInputAction(this._leftUp.bind(this), Cesium.ScreenSpaceEventType.LEFT_UP),
            e.setInputAction(this._mouseMove.bind(this), Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            ,this.active = !0, Cesium.defined(this._activeEditor) ? this._activeEditor.active = !0 : this.setModeTranslation()
        ,t.requestRenderMode && t.requestRender()
    }
    ,
    TransformEditorViewModel.prototype.deactivate = function() {
        var e = this._sseh
            , t = this._scene;
        e.removeInputAction(this._leftDown.bind(this), Cesium.ScreenSpaceEventType.LEFT_DOWN),
            e.removeInputAction(this._leftUp.bind(this), Cesium.ScreenSpaceEventType.LEFT_UP),
            e.removeInputAction(this._mouseMove.bind(this), Cesium.ScreenSpaceEventType.MOUSE_MOVE),
            this.active = !1,
        Cesium.defined(this._activeEditor) && (this._activeEditor.active = !1),
        t.requestRenderMode && t.requestRender()
    }
    ,
    TransformEditorViewModel.prototype.expandMenu = function() {
        this.menuExpanded = !0
    }
    ,
    TransformEditorViewModel.prototype.setModeTranslation = function() {
        this.editorMode = EditorMode.TRANSLATION,
            this.menuExpanded = !1
    }
    ,
    TransformEditorViewModel.prototype.setModeRotation = function() {
        this.editorMode = EditorMode.ROTATION,
            this.menuExpanded = !1
    }
    ,
    TransformEditorViewModel.prototype.setModeScale = function() {
        this.editorMode = EditorMode.SCALE,
            this.menuExpanded = !1
    }
    ,
    TransformEditorViewModel.prototype.toggleNonUniformScaling = function() {
        this.enableNonUniformScaling = !this.enableNonUniformScaling
    }
    ,
    TransformEditorViewModel.prototype._leftDown = function(e) {
        this._activeEditor.handleLeftDown(e.position);
        var t = this._scene;
        t.requestRenderMode && t.requestRender()
    }
    ,
    TransformEditorViewModel.prototype._mouseMove = function(e) {
        this._activeEditor.handleMouseMove(e.endPosition);
        var t = this._scene;
        t.requestRenderMode && t.requestRender()
    }
    ,
    TransformEditorViewModel.prototype._leftUp = function(e) {
        this.menuExpanded = !1,
            this._activeEditor.handleLeftUp(e.position);
        var t = this._scene;
        t.requestRenderMode && t.requestRender()
    }
    ,
    TransformEditorViewModel.prototype._update = function() {
        if (this.active) {
            this._activeEditor.update();
            var e = this._scene
                , t = getWidgetOrigin(this._transform, this._originOffset, widgetPosition)
                , i = Cesium.SceneTransforms.wgs84ToWindowCoordinates(e, t, screenPosition);
            Cesium.defined(i) && (this.left = Math.floor(i.x - 13) + "px",
                this.top = Math.floor(i.y) + "px")
        }
    }
    ,
    TransformEditorViewModel.prototype.isDestroyed = function() {
        return !1
    }
    ,
    TransformEditorViewModel.prototype.destroy = function() {
        this.deactivate(),
            this._sseh.destroy(),
            this._rotationEditor.destroy(),
            this._translationEditor.destroy(),
            this._scaleEditor.destroy(),
            this._removePostUpdateEvent(),
           Cesium.destroyObject(this)
    }
    ,
    TransformEditorViewModel.EditorMode = EditorMode;

export default TransformEditorViewModel;